﻿using IMark.DAO;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IMark
{
    public partial class SanPham : System.Web.UI.Page
    {
        public string dsSanPham;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["IDLoaiSP"] != null)
            {
                long IDLoaiSanPham = long.Parse(Request.QueryString["IDLoaiSP"].ToString());
                List<SAN_PHAM> ltSanPham = SanPhamModels.LaySanPhamTheoLoai(IDLoaiSanPham, new List<SAN_PHAM>());
                if (!IsPostBack)
                {
                    
                }
                else
                {
                    string parameter = Request["__EVENTARGUMENT"];
                    if (parameter == "SearchPrice")
                    {
                        long Min = long.Parse(hdfMin.Value);
                        long Max = long.Parse(hdfMax.Value);
                        ltSanPham = ltSanPham.Where(w => w.DonGia >= Min && w.DonGia <= Max).ToList();
                    }
                    else if (parameter == "Clear")
                    {
                        ltSanPham = SanPhamModels.LaySanPhamTheoLoai(IDLoaiSanPham, new List<SAN_PHAM>());
                    }
                }
                ListView1.DataSource = ltSanPham;
                ListView1.DataBind();
            }
            
        }
        protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            Label BigNewprice = e.Item.FindControl("newPrice") as Label;
            Label SmallPrice = e.Item.FindControl("lblSmallNewPrice") as Label;
            Label Rating = e.Item.FindControl("rating") as Label;
            Label Status = e.Item.FindControl("lblStatus") as Label;

            SAN_PHAM h = e.Item.DataItem as SAN_PHAM;
            Status.Text = "HOT";
            if(h.NgayThem.Year==DateTime.Now.Year && h.NgayThem.Month == DateTime.Now.Month)
                Status.Text = "NEW";

            if (h.NgayBatDau<=DateTime.Now && h.NgayKetThuc>=DateTime.Now)
            {
                BigNewprice.Text = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0:0,0}", h.DonGia - (h.DonGia * h.GiamGia) / 100) + " đ";
                SmallPrice.Text = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0:0,0}", h.DonGia - (h.DonGia * h.GiamGia) / 100) + " đ";
                Status.Text = "SALE";
            }
            else
            {
                BigNewprice.Text = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0:0,0}", h.DonGia) + " đ";
                SmallPrice.Text = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0:0,0}", h.DonGia) + " đ";
            }
            Rating.Text = Utils.GenerateRation(h.DANH_GIA.ToList());
            Image Image = e.Item.FindControl("SpecIcon") as Image;
            Image.ImageUrl = "images/products/" + h.HINH_SAN_PHAM.First().Hinh;
        }

        protected void btnTimKiem_Click(object sender, EventArgs e)
        {
            Response.Write("adsf");
        }
    }
}